package com.zbxx.practice.designpattern.watch.normal;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * 目标对象
 *
 * @author 万仁杰
 * @date 2019/06/04
 */
public class Subject {

    /**
     * 观察者对象
     */
    private List<Observer> observers = new CopyOnWriteArrayList<>();

    /**
     * 添加新的
     * @param observer
     */
    public void add(Observer observer){
        observers.add(observer);
    }

    /**
     * 删除观察者
     * @param observer
     */
    public void remove(Observer observer){
        observers.remove(observer);
    }

    public void notifyObservers(){
        observers.forEach(observer -> observer.update(this));
    }
}
